package com.android.cl.coludtf.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.android.cl.coludtf.R;
import com.android.cl.coludtf.activity.base.BaseActivity;
import com.android.cl.coludtf.fragment.Address;
import com.android.cl.coludtf.fragment.Message;
import com.android.cl.coludtf.fragment.My;
import com.android.cl.coludtf.fragment.Worktop;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Main extends BaseActivity {
    @BindView(R.id.lay_frame)
    FrameLayout layFrame;
    @BindView(R.id.LL_Msg)
    LinearLayout LLMsg;
    @BindView(R.id.LL_Txl)
    LinearLayout LLAddress;
    @BindView(R.id.LL_Worktop)
    LinearLayout LLWorktop;
    @BindView(R.id.LL_My)
    LinearLayout LLMy;
    @BindView(R.id.Img_Msg)
    ImageView ImgMsg;
    @BindView(R.id.Img_Txl)
    ImageView ImgTxl;
    @BindView(R.id.Img_Worktop)
    ImageView ImgWorktop;
    @BindView(R.id.Img_My)
    ImageView ImgMy;
    @BindView(R.id.Txt_Msg)
    TextView TxtMsg;
    @BindView(R.id.Txt_Txl)
    TextView TxtTxl;
    @BindView(R.id.Txt_Worktop)
    TextView TxtWorktop;
    @BindView(R.id.Txt_My)
    TextView TxtMy;
    private FragmentTransaction ftr;//事务
    /**
     * 消息
     */
    private Fragment message;
    /**
     * 通讯录
     */
    private Fragment address;
    /**
     * 工作台
     */
    private Fragment worktop;
    /**
     * 我的
     */
    private Fragment my;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        InitData();
    }

    private void InitData() {
        resetImg();
/**
 * 0:消息，message；1：通讯录，address;2:工作台，worktop;3:我的，my
 */
        setSelected(0);
    }

    @OnClick({R.id.LL_Msg, R.id.LL_Txl, R.id.LL_Worktop, R.id.LL_My})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.LL_Msg:
                //消息
                setSelected(0);
                break;
            case R.id.LL_Txl:
                //通讯录
                setSelected(1);
                break;
            case R.id.LL_Worktop:
                //工作台
                setSelected(2);
                break;
            case R.id.LL_My:
                //我的
                setSelected(3);
                break;
        }
    }

    //自定义一个方法，设定布局中间的FrameLayout的选择状态
    @SuppressLint("ResourceAsColor")
    private void setSelected(int fra) {
        resetImg();
        int frame = R.id.lay_frame;
        //需要将按钮变亮，且需要切换fragment的状体
        //获取事务
        FragmentManager fm = getSupportFragmentManager();
        ftr = fm.beginTransaction();//开启一个事务
        hideTransaction(ftr);//自定义一个方法，来隐藏所有的fragment

        if (fra == 0) {
            if (message == null) {
                //实例化每一个fragment
                message = new Message();
                //千万别忘记将该fragment加入到ftr中
                ftr.add(frame, message);
            }
            ftr.show(message);

            ImgMsg.setImageResource(R.mipmap.msg2);
            TxtMsg.setTextColor(Color.BLUE);
        } else if (fra == 1) {
            if (address == null) {
                address = new Address();
                ftr.add(frame, address);
            }
            ftr.show(address);
            TxtTxl.setTextColor(Color.BLUE);
            ImgTxl.setImageResource(R.mipmap.txl2);
        } else if (fra == 2) {
            if (worktop == null) {
                worktop = new Worktop();
                ftr.add(frame, worktop);
            }
            ftr.show(worktop);
            TxtWorktop.setTextColor(Color.BLUE);
            ImgWorktop.setImageResource(R.mipmap.work2);
        } else if (fra == 3) {
            if (my == null) {
                my = new My();
                ftr.add(frame, my);
            }
            ftr.show(my);
            TxtMy.setTextColor(Color.BLUE);
            ImgMy.setImageResource(R.mipmap.my2);
        }
        ftr.commit();//最后千万别忘记提交事务
    }

    //隐藏fragment
    private void hideTransaction(FragmentTransaction ftr) {

        if (message != null) {
            ftr.hide(message);//隐藏该fragment
        }
        if (address != null) {
            ftr.hide(address);
        }
        if (worktop != null) {
            ftr.hide(worktop);
        }
        if (my != null) {
            ftr.hide(my);
        }
    }

    //复位按钮，即设置按钮为暗色
    private void resetImg() {
        ImgMsg.setImageResource(R.mipmap.msg);
        ImgTxl.setImageResource(R.mipmap.txl);
        ImgWorktop.setImageResource(R.mipmap.work);
        ImgMy.setImageResource(R.mipmap.my);

        TxtMsg.setTextColor(Color.BLACK);
        TxtTxl.setTextColor(Color.BLACK);
        TxtWorktop.setTextColor(Color.BLACK);
        TxtMy.setTextColor(Color.BLACK);
//        TxtMsg.setTextColor(Color.BLUE);
//        TxtTxl.setTextColor(Color.BLUE);
//        TxtWorktop.setTextColor(Color.BLUE);
//        TxtMy.setTextColor(Color.BLUE);
    }
}
